package com.to3ks.excelservice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.to3ks.common.base.result.R;
import com.to3ks.excelservice.entity.XlsSwry;
import com.to3ks.excelservice.service.XlsSwryService;
import com.to3ks.excelservice.utils.ImportExcelUtils;
import com.to3ks.excelservice.vo.XlsSwryQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 税务 前端控制器
 * </p>
 *
 * @author Tigger
 * @since 2021-01-08
 */


@Api("Excel导入导出管理")
@RestController
@RequestMapping("/excel")
@Slf4j
public class XlsSwryController {


    @Resource
    private XlsSwryService xlsSwryService;


    @ApiOperation("列出所有税务人员信息")
    @GetMapping("allswry")
    public R listAllSwry(){

        List<XlsSwry> lists = xlsSwryService.list();
        return R.ok().data("list",lists);

    }


    @ApiOperation("条件查询税务人员")
    @GetMapping("xlstodb")
    public R testXlsToDb(HttpServletRequest httpServletRequest) throws Exception {
        long startTime = System.currentTimeMillis();
        httpServletRequest.setAttribute("xlsFile=","G:\\税务人员.xlsx");
        ImportExcelUtils.saxReadListStringV2007A(httpServletRequest,xlsSwryService);
        long endTime = System.currentTimeMillis();
        log.info("<=**********向数据库导入数据总耗时:{}秒**********=>",(endTime-startTime)/1000);

        return R.ok();
    }

    @ApiOperation("税务人员综合查询并分页列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                      @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
                      @ApiParam("税务人员列表查询对象") XlsSwryQueryVo xlsSwryQueryVo){

        Page<XlsSwry> pageParam = new Page<>(page, limit);
        IPage<XlsSwry> pageModel = xlsSwryService.selectPage(pageParam, xlsSwryQueryVo);
        List<XlsSwry> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("根据id选出指定税务人员")
    @GetMapping("getswryxm/{swryxm}")
    public R getSwryxm(@ApiParam(value = "税务人员swryxm", required = true) @PathVariable String swryxm){
        List<Map<String, Object>> lists = xlsSwryService.selectNameList(swryxm);
        if(lists != null){
            return R.ok().data("item", lists);
        }else{
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("根据id选出指定税务人员")
    @GetMapping("getid/{id}")
    public R getXlsSwryId(@ApiParam(value = "税务人员ID", required = true) @PathVariable String id){
        XlsSwry xlsSwry = xlsSwryService.getById(id);
        if(xlsSwry != null){
            return R.ok().data("item", xlsSwry);
        }else{
            return R.error().message("数据不存在");
        }
    }


    @ApiOperation(value = "根据ID列表删除税务人员")
    @DeleteMapping("batch-remove")
    public R removeRows(
            @ApiParam(value = "税务人员ID列表", required = true)
            @RequestBody List<String> idList){
        log.info("***********removeRows方法执行"+idList.toString());
        boolean result = xlsSwryService.removeByIds(idList);
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("数据不存在");
        }
    }

}

